package com.ruoyi.project;


import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.project.device.space.service.IBasCargoSpaceService;
import com.ruoyi.project.deviceInfo.service.IMsgDdjService;
import com.ruoyi.project.mydo.mydojob.service.IMydoJobService;
import com.ruoyi.project.viewin.domain.ViewInJobDetail;
import com.ruoyi.project.viewin.service.IViewInJobDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/main")
@RestController
public class MainController extends BaseController {

    @Autowired
    private IMydoJobService mydoJobService;



    @Autowired
    private IBasCargoSpaceService basCargoSpaceService;

    @Autowired
    private IMsgDdjService msgDdjService;

    @Autowired
    private IViewInJobDetailService jobDetailService;


    @RequestMapping("/jobList")
    public HashMap<String, String> jobList(String warehouseId) {
        HashMap<String, String> map = new HashMap<>();
        Integer count = mydoJobService.selectByInCount();
        Integer count2 = mydoJobService.selectByInCount2();
        Integer outCount = mydoJobService.selectByOutCount();
        Integer outCount2 = mydoJobService.selectByOutCount2();
        Integer cargoInCount = basCargoSpaceService.selectByInCount();
        Integer countSum = basCargoSpaceService.selectByCountSum();  //托盘库总货位数量
        Integer countSum2 = basCargoSpaceService.selectByCountSum2();//料箱库总货位数量
        Integer cargoOutCount = basCargoSpaceService.selectByOutCount();
        Integer KtpCount = basCargoSpaceService.selectByKtpCount();  //托盘库空货位数量
        Integer KtpCount2 = basCargoSpaceService.selectByKtpCount2(); //料箱库空货位数量
        map.put("inJob", String.valueOf(count));
        map.put("inJob2", String.valueOf(count2));
        map.put("outJob", String.valueOf(outCount));
        map.put("outJob2", String.valueOf(outCount2));
        map.put("pastInJob", String.valueOf(cargoInCount));
        map.put("pastOutJob", String.valueOf(cargoOutCount));
        map.put("KtpNUm", String.valueOf(KtpCount));//托盘库空货位数量
        map.put("KtpNUm2", String.valueOf(KtpCount2));//料箱库空货位数量
        map.put("countSum", String.valueOf(countSum)); //托盘库总货位数量
        map.put("countSum2", String.valueOf(countSum2));//料箱库总货位数量

        return map;
    }


    @RequestMapping("/findMinutesRecord")
    public Map<String, String[]> findMinutesRecord(String warehouseId) {
        String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        Map<String, String[]> map = new HashMap<>();
        String[] result = new String[48];
        String[] dates = new String[48];
        String beginDate = "";
        String finishDate = "";
        Integer hour = 0;
        String minute = "";
        for (int i = 0; i < 48; i++) {
            if (i < 20) {
                if (i % 2 == 0) {
                    hour = i / 2;
                    minute = ":00:00";
                    beginDate = date + " 0" + hour + minute;
                    finishDate = date + " 0" + hour + ":29:59";
                    dates[i] = "0" + hour + minute.substring(0, 3);
                } else {
                    hour = i / 2;
                    minute = ":30:00";
                    beginDate = date + " 0" + hour + minute;
                    finishDate = date + " 0" + hour + ":59:59";
                    dates[i] = "0" + hour + minute.substring(0, 3);
                }
            } else {
                if (i % 2 == 0) {
                    hour = i / 2;
                    minute = ":00:00";
                    beginDate = date + " " + hour + minute;
                    finishDate = date + " " + hour + ":29:59";
                    dates[i] = hour + minute.substring(0, 3);
                } else {
                    hour = i / 2;
                    minute = ":30:00";
                    beginDate = date + " " + hour + minute;
                    finishDate = date + " " + hour + ":59:59";
                    dates[i] = hour + minute.substring(0, 3);
                }
            }
            Integer num = 0;
            if ("1".equals(warehouseId)) {
                num = mydoJobService.selectJobNumByTime(beginDate, finishDate,"1");

            } else {
                //实体类建设完成后修改4B库
                num = mydoJobService.selectJobNumByTime(beginDate, finishDate,"2");

            }
            if (num == null) {
                result[i] = "0";
            } else {
                result[i] = String.valueOf(num);
            }
        }
        map.put("dates", dates);
        map.put("data", result);
        return map;
    }


    @RequestMapping("/listJobIng")
    public TableDataInfo listJobIng(String warehouseId) {
//        startPage();
        ViewInJobDetail viewInJobDetail=new  ViewInJobDetail();
        List<ViewInJobDetail>  viewInJobDetails= null;
        if (warehouseId.equals("1")) {
            viewInJobDetail.setRoadwayId("1");
            viewInJobDetails = jobDetailService.selectViewInJobDetailList(viewInJobDetail);
        } else {
            viewInJobDetail.setRoadwayId("2");
            viewInJobDetails = jobDetailService.selectViewInJobDetailList(viewInJobDetail);
        }

        return getDataTable(viewInJobDetails);
    }


    @RequestMapping("/findDevInfo")
    public Map<String, String> findDevInfo() {
        Map<String, String> map = new HashMap<>();
        int countOnline = 0;
        int countWork = 0;
        int countWarn = 0;
        int countSum = 0;


        countOnline = msgDdjService.selectAllCount();
        countWork = msgDdjService.selectByYx();
        countWarn = msgDdjService.selectYc();

        String onlinePer = getPercent(countOnline, 2);
        String workPer = getPercent(countWork, 2);
        String warnPer = getPercent(countWarn, 2);

        map.put("online", String.valueOf(countOnline));
        map.put("work", String.valueOf(countWork));
        map.put("warn", String.valueOf(countWarn));
        map.put("onlinePer", onlinePer);
        map.put("workPer", workPer);
        map.put("warnPer", warnPer);

        return map;
    }

    public static String getPercent(int x, int y) {
        double d1 = x * 1.0;
        double d2 = y * 1.0;
        NumberFormat percentInstance = NumberFormat.getPercentInstance();
        // 设置保留几位小数，这里设置的是保留两位小数
        percentInstance.setMinimumFractionDigits(2);
        return percentInstance.format(d1 / d2);
    }


}
